/**
 * @description 理财/资产计划持仓表
 * @author wangjianjun
 */

import React, { PureComponent } from 'react';
import { TableC, ExportC } from '../../../../comps';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col } from 'antd';
import { connect } from 'dva';
import TotalAccount from '../common/TotalAccount';
import SetColumns from '../common/SetColumns';
import _ from 'lodash';

@Form.create()
@connect(({ positionInquiryM, loading }) => ({
  positionInquiryM,
  loading: loading.effects['positionInquiryM/assetplanTable'],
}))
export default class Assetplan extends PureComponent {
  constructor(props) {
    super(props);
    this.scrollx = 0;
    this.newColumns = [];
    this.state = {
      search: {},
      columns: [],
      dataSource: [],
      page: 1,
      pageSize: 20,
      total: 0,
      account: {},
      treeData: [],
      checkedKeys: [],
      scrollx: 0,
    };
  }
  componentDidMount() {
    const { dispatch } = this.props;
    //债券默认查表头信息
    dispatch({
      type: 'positionInquiryM/queryShowCol',
      payload: {
        type: 'Trade_Ana_Fmt_Hold_Info',
      },
      callback: (data) => {},
    });
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const init = !this.props.assetplanArgs ? '' : JSON.stringify(this.props.assetplanArgs);
    const initN = !nextProps.assetplanArgs ? '' : JSON.stringify(nextProps.assetplanArgs);
    if (init != initN) {
      this.setState(
        {
          search: nextProps.assetplanArgs,
        },
        () => {
          this.getTableData();
        },
      );
    }
  }
  //分页切换
  handleTableChange = (pagination, filter, sorter) => {
    const { current, pageSize } = pagination;
    this.setState(
      {
        page: current,
        pageSize: pageSize,
      },
      () => {
        this.getTableData('pageChange');
      },
    );
  };
  //查询表格数据
  getTableData = (type) => {
    const { dispatch } = this.props;
    const { page, pageSize, search } = this.state;
    const params = {};
    params.pageSize = pageSize;
    if (type != 'pageChange') {
      //点击查询按钮 重置页码
      this.setState({
        page: 1,
      });
      params.page = 1;
    } else {
      //点击页码切换时候调用
      params.page = page;
    }
    //列表数据
    dispatch({
      type: 'positionInquiryM/assetplanTable',
      payload: {
        ...params,
        ...search,
      },
      callback: (res) => {
        res.data &&
          res.data.map((item, key) => {
            item.key = key;
          });
        this.setState({
          dataSource: res.data,
          total: res.total,
        });
      },
    });

    if (type != 'pageChange') {
      //获取统计信息
      dispatch({
        type: 'positionInquiryM/getSearchTotal',
        payload: {
          ...search,
          type: '5',
        },
        callback: (data) => {
          this.setState({
            account: data,
          });
        },
      });
    }
  };

  // 导出参数调整
  changeSearch() {
    const { search } = this.state;
    let newSearch = _.cloneDeep(search);
    if (newSearch && newSearch.fundId) {
      newSearch.fundId = newSearch.fundId.join(',');
    }
    return newSearch;
  }

  render() {
    const {
      positionInquiryM: { columnsInfo, checkedKeysInfo },
      loading,
    } = this.props;
    const { search } = this.state;
    const columns = _.cloneDeep(columnsInfo[5].columns);
    this.newColumns = columns.filter((item) => item.display == true);
    if (this.newColumns && this.newColumns.length > 10) {
      const a = this.newColumns.length;
      this.scrollx = a * 150;
    } else {
      this.scrollx = 0;
    }

    this.newColumns.map((item) => {
      if (item.key == 'costPrice' || item.key == 'enLastPrice' || item.key == 'marketValue' || item.key == 'enFloatProfit') {
        item.type = 'money';
      }

      if (item.key == 'lcurrentAmount') {
        item.align = 'right';
        item.type = 'number';
      }
    });
    return (
      <div>
        <Row>
          <Col span={21}>
            <TotalAccount account={this.state.account} type='0' />
          </Col>

          <Col span={3}>
            <Row>
              <Col span={10} style={{ textAlign: 'right' }}>
                <ExportC style={{ marginTop: '10px' }} url='api/v1/tradeAnaFmtHoldInfo/exportFmt' search={this.changeSearch()} />
              </Col>
              <Col span={10} style={{ marginTop: '10px', marginLeft: '3px', textAlign: 'right' }}>
                <SetColumns type='5' parameter='Trade_Ana_Fmt_Hold_Info' checkedKeysInfo={checkedKeysInfo} />
              </Col>
            </Row>
          </Col>
        </Row>
        <TableC
          columns={this.newColumns || []}
          dataSource={this.state.dataSource || []}
          onChange={this.handleTableChange}
          current={this.state.page}
          pageSize={this.state.pageSize}
          total={this.state.total}
          scroll={{ x: this.scrollx }}
          loading={loading}
          rowKey={'key'}
        />
      </div>
    );
  }
}
